package cn.tedu.bailianagent.controller;

import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentRagOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 非流式调用：调用阿里百炼大模型
 * 1.请求URL地址：/ai/bailian/agent/call
 * 2.请求方法：GET
 * 3.请求参数：message（用户输入的消息）
 * 4.参数类型：查询字符串
 * 5.返回结果：String 模型返回的答案
 */
@Slf4j
@RestController
@RequestMapping("/ai")
public class BaiLianAgentController {
    //创建DashScopeAgent对象，用于和百炼进行交互
    private DashScopeAgent agent;
    @Value("${spring.ai.dashscope.agent.options.app-id}")
    private String appId;
    //构造方法注入
    public BaiLianAgentController(DashScopeAgentApi dashScopeAgentApi) {
        this.agent = new DashScopeAgent(dashScopeAgentApi);
    }





    @GetMapping("/bailian/agent/call")
    public String call(String message){
        log.debug("调用阿里百炼大模型，参数：{}",message);
        log.debug("appId={}",appId);
        ChatResponse response = agent.call(new Prompt(message
        , DashScopeAgentOptions.builder().withAppId(appId).build()));
        log.debug("阿里百炼大模型返回结果：{}",response);
        //从返回结果中提取模型的回答
        String content = response.getResult().getOutput().getText();

        return content;
    }
}
